package com.web;

import com.pojo.Root;
import com.service.RootService;
import com.service.impl.RootServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author hjb
 * @Version 1.0
 * @Contact 1754729389@qq.com
 */
public class RootServlet extends BaseServlet {

    private RootService rootService = new RootServiceImpl();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Root root = rootService.login(new Root(username,password));
        if(root != null){
            // 登录 成功
            // 保存用户登录的信息到Session域中
            req.getSession().setAttribute("root", root);
            //跳到成功页面login_success.html
            req.getRequestDispatcher("/pages/root/login_success.jsp").forward(req, resp);
        }else{
            // 把错误信息，和回显的表单项信息，保存到Request域中
            req.setAttribute("msg", "用户或密码错误！");
            req.setAttribute("username", username);
            //   跳回登录页面
            req.getRequestDispatcher("/pages/root/login.jsp").forward(req, resp);
        }
    }
}
